package org.dromara.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.mall.domain.MallCateBrand;
import org.dromara.mall.mapper.MallCateBrandMapper;
import org.dromara.mall.service.IMallCateBrandService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 分类品牌Service业务层处理
 *
 * @author Lion Li
 * @date 2024-05-19
 */
@RequiredArgsConstructor
@Service
public class MallCateBrandServiceImpl implements IMallCateBrandService {

    private final MallCateBrandMapper baseMapper;

    /**
     * 查询分类品牌
     */
    @Override
    public MallCateBrand queryById(Long id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询分类品牌列表
     */
    @Override
    public TableDataInfo<MallCateBrand> queryPageList(MallCateBrand bo, PageQuery pageQuery) {
        Page<MallCateBrand> result = baseMapper.queryPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询分类品牌列表
     */
    @Override
    public List<MallCateBrand> queryList(MallCateBrand bo) {
        LambdaQueryWrapper<MallCateBrand> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<MallCateBrand> buildQueryWrapper(MallCateBrand bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallCateBrand> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCateId() != null, MallCateBrand::getCateId, bo.getCateId());
        lqw.eq(bo.getBrandId() != null, MallCateBrand::getBrandId, bo.getBrandId());
        return lqw;
    }

    /**
     * 新增分类品牌
     */
    @Override
    public Boolean insertByBo(MallCateBrand bo) {
        MallCateBrand add = MapstructUtils.convert(bo, MallCateBrand.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改分类品牌
     */
    @Override
    public Boolean updateByBo(MallCateBrand bo) {
        MallCateBrand update = MapstructUtils.convert(bo, MallCateBrand.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MallCateBrand entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除分类品牌
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
    /*根据分类ID查询品牌*/
//    @Override
//    public List<MallCateBrand> findBrandByCateId(Long cateId) {
//        return baseMapper.findBrandByCateId(cateId);
//    }

}
